package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import DAO.TrainManageDAO;
import entity.Train;

/**
 * Servlet implementation class InsertTrainAction
 */
public class InsertTrainAction extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public InsertTrainAction() {
		super();
		// TODO Auto-generated constructor stub
	}
	@Override
	public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.service(arg0, arg1);
		//arg1.getWriter().append("Served at:guocl");
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// response.getWriter().append("Served at:
		// ").append(request.getContextPath());
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		// doGet(request, response);
		response.setCharacterEncoding("UTF-8"); // 设置输出编码
		request.setCharacterEncoding("UTF-8");

		try {
			// 插入数据库
			String m_trainid = request.getParameter("trainid");
			String m_start = request.getParameter("start");
			String m_end = request.getParameter("end");
			String m_time = request.getParameter("time");
			String m_yz = request.getParameter("yzprice");
			Float m_yzprice = Float.parseFloat(m_yz);
			String m_rz = request.getParameter("rzprice");
			Float m_rzprice = Float.parseFloat(m_rz);
			String m_yw = request.getParameter("ywprice");
			Float m_ywprice = Float.parseFloat(m_yw);
			String m_rw = request.getParameter("rwprice");
			Float m_rwprice = Float.parseFloat(m_rw);
			String m_root = request.getParameter("root");

			Train train = new Train();
			train.setTrainid(m_trainid);
			train.setStart(m_start);
			train.setEnd(m_end);
			train.setTime(m_time);
			train.setYzprice(m_yzprice);
			train.setRzprice(m_rzprice);
			train.setYwprice(m_ywprice);
			train.setRwprice(m_rwprice);
			train.setRoot(m_root);

			int success = TrainManageDAO.insert(train);
			if(success==1){
				System.out.println("添加成功");
			}
			request.setAttribute("success", success);
			// 设置全路径 否则trainManage.jsp跳转到servlet/trainManage.jsp路径下不存在
			response.sendRedirect("http://localhost/TrainDatabase/jsp/trainManage.jsp");

		} catch (Exception e) {
			System.out.println("错误：" + e.getMessage());
			response.sendRedirect("http://localhost/TrainDatabase/jsp/trainManage.jsp");
		}
	}

	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		super.destroy();
	}
}
